$(function(){
	$("#ibaby_sms_template").click(function(){
		send_request(function(data){
			 $("div.jw_temp_sms").html(data);
		 }, "/control/smsTemplateLists.htm", true);
	});
	
	 $("#J_check_sms").click(function(){
		var sms_user = $("#J_selects").find('option:selected').val();
		var sms_mintime = $("#d4311").val();
		var sms_maxtime = $("#d4312").val();
		if(sms_user==0){
			
			 send_request(function(data){
				 $("div.jw_content_sms").html(data);
			 }, "/control/smsList.htm", true);
			 $.post("/control/gainPeopleByPlace.htm",{"classOrDemp" : "demp_"},function(data){
				    var Obj_ = eval('(' + data + ')');
				    $("div.jw_toolbar").find("select").empty();
				    $("div.jw_toolbar").find("select").append("<option value='0' selected='selected'>全部</option>");
				    $.each( Obj_ ,function(i , entity){
				    	
				    	$("div.jw_toolbar").find("select").append("<option value="+entity[0]+">"+entity[1]+"</option>");
				    });
				    
				  });
			 return;
		}
		$.post("/control/findSms.htm", { "sms_user": sms_user, minDate: sms_mintime ,maxDate:sms_maxtime},
				   function(data){
			       $("div.jw_content_sms").html(data);
		});
	 });
	
     $("#J_sends_sms_").click(function(){
    	$(this).text("发送中");
		var user_info = $(".J_sms_man_").val();
		var content = editor.text();
		var sign = $(".tex").val();
	    if(user_info==""){
	    	alert("接收人不能为空 ！");
	    	return ;
	    }
	    if(content==""){
	    	alert("发送内容不能为空！");
	    	return ;
	    }
	    $.post("/control/sendSms.htm", { "userInfo": user_info , "content" : content , "sign" : sign },function(data){
	    	$(this).text("发送完");
	    	var jsonData = eval('(' + data + ')');
	    	if(jsonData.tishi=="failed"){
	    		alert("您的短信量不够，请联系客服充值！");return;
	    	}
	    	//alert(data);
	    	if(jsonData.error=="error"){
	    		alert("您所有短信不能全部发送成功，具体在短信错误列表查看，若重发失败则联系我们的客服！");
	    	}
	    	
		  });
	});
	$("#J_sends_sms_time").click(function(){
		alert("功能内测中...");
	});
	$("#savatemsign").click(function(){
	_tex = $(".tex").val();
	_id = $(".tex_id").val();
	$.post("/control/savaSign.htm", { "text": _tex , "id" : _id},function(data){
		if(data){alert("保存成功！");}
		else{
			alert("保存失败");
		}
	  });
	});
	
    $("a#sel_temp_").click(function(){
		get_request(function(temp){
			var sel = $("#J_table_");
			sel.html("");
			sel.append('<tr id="J_temp_tr"><td class="J_td_box">选择模板</td><td class="J_td_id">模板编号</td><td class="J_td_text">模板内容</td></tr>');
			var jsontemp = eval('(' + temp + ')');
			$.each(jsontemp , function(i , date){
				
				var html = "<tr id='J_temp_tr'>"
					      +"<td class='J_td_box'width='70' style=' border: 1px #ccc solid; padding:5px; '><input type='radio' name='radio' value='"+date.content+"'></td>"
					      +"<td class='J_td_id' id='"+date.id+"' width='70' style=' border: 1px #ccc solid; padding:5px; '>"+date.id+"</td>"
				          +"<td class='J_td_text' title='"+date.content+"' width='350' style=' border: 1px #ccc solid; padding:5px; '>"+date.content+"</td>";
				          +"</tr>";
				sel.append(html);
			});
			
			var wBox=$("#wbox8").wBox({
			    
				title: "选择模板",
				target:"#J_temp_show"
				});
		    wBox.showBox();
		
//			alert("为了做出更加适应您的模板，模板还在内测中...");
		 }, "/control/sms_getTemplate.htm" , true);
     });
	$("#J_select_tmp_").click(function(){
		var i_txt = $("input[name='radio']:checked").val();
		if(i_txt==""){
			alert("选择模板不能为空！");
			return;
		}
		editor.html(i_txt);
	 });
   _init_url = "/control/sms_getPerson.htm";
   send_request(function(data){
		 var userObj_ = eval('(' + data + ')');
		 var setting = {
					check: {
						enable: true
					},
					data: {
						simpleData: {
							enable: true,
							idKey: "id",
							pIdKey: "pId",
							rootPId: 0
								}
					},
					 
					 callback: {
						onCheck: onCheck
					 }
					
				};
		 $.fn.zTree.init($("#treeDemo"), setting, userObj_[0].class_);
		 $.fn.zTree.init($("#treeDemo2"), setting, userObj_[0].demp_);
		 },_init_url, true);
         function onCheck(e, treeId, treeNode) {
        	 var zTree = $.fn.zTree.getZTreeObj(treeId);
     		 checkCount = zTree.getCheckedNodes(true);
     		 var arrys = new Array();
     		 $.each(checkCount , function(i ,data){
                 if(data.id>0){
                   arrys.push(data.name+"#"+data.phone);
                 }
     		 });
     		 $(".J_sms_man_").val(arrys);
	     }
});
var editor;
KindEditor.ready(function(K) {
editor = K.create('#textarea', {
	afterChange : function() {
		K('.word_count').html(this.count('text'));
	},
	items:[ 'cut', 'copy', 'paste'],
	allowUpload : true,
	allowFileManager : true
});
$("#ibaby_sms_report").click(function(){
	 send_request(function(data){
		 $("div.jw_content_sms").html(data);
	 }, "/control/smsList.htm", true);
	 $.post("/control/gainPeopleByPlace.htm",{"classOrDemp" : "demp_"},function(data){
		    var Obj_ = eval('(' + data + ')');
		    $("div.jw_toolbar").find("select").empty();
		    $("div.jw_toolbar").find("select").append("<option value='0'>全部</option>");
		    $.each( Obj_ ,function(i , entity){
		    	
		    	$("div.jw_toolbar").find("select").append("<option value="+entity[0]+">"+entity[1]+"</option>");
		    });
		    
		  });
});
$("a#addTem").click(function(){
	$("#addTemDisplay").show("slow");
});
$("#addTemOK").click(function(){
	var content = $("#addTemContent").val();
	 if(content==""){
		 alert("内容不能为空！");return;
	 }
	 $.post("/control/smsTemplateAdd.htm",
			 {"content" : content},function(data){
				 $("#addTemContent").val("");
				 $("#addTemDisplay").hide(); 
				 send_request(function(data){
					 $("div.jw_temp_sms").html(data);
				 }, "/control/smsTemplateLists.htm", true);
			 });
});


});
function deleteTem(id){
	if(confirm("确定删除!")){
		send_request(function(data){
			alert("删除成功！");
			 $("tr#s"+id).hide("slow");
		 }, "/control/smsTemplateDel.htm?id="+id, true);
	}
}

function toTempage(page){
	send_request(function(data){
		 $("div.jw_temp_sms").html(data);
	 }, "/control/smsTemplateLists.htm?pageNum="+page, true);
}

function topage(page , int) {
	if(int==0){
		url = "/control/smsList.htm?page="+page;
	}
	if(int==1){
		var sms_user = $("#J_selects").find('option:selected').val();
		var sms_mintime = $("#d4311").val();
		var sms_maxtime = $("#d4312").val();
		url = "/control/findSms.htm?page="+page+"&sms_user="+sms_user+"&minDate="+sms_mintime+"&maxDate="+sms_maxtime;
	}
 send_request(function(data){
	 $("div.jw_content_sms").html(data);
 }, url, true);
}
